package com.ttuproject.ttu.service.serviceImpl;

import com.ttuproject.ttu.dao.User;
import com.ttuproject.ttu.exception.Advice;
import com.ttuproject.ttu.exception.TTUException;
import com.ttuproject.ttu.mapper.TTUMapper.UserMapper;
import com.ttuproject.ttu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public int Userlogin(String userName, String passWord) {
        User user=userMapper.selectUser(userName, passWord);
        if (user != null && user.getPower() == 1) {
            //管理员用户
            return user.getPower();
        }else if (user !=null&&user.getPower()==2) {
            //普通用户
            return user.getPower();
        } else {
            throw new TTUException(Advice.LOGIN_ERROR);
        }
    }

    @Override
    public boolean register(String userName, String passWord) {
        //注册默认为用户，若想要提升权限需要管理员修改
        Boolean registerType=userMapper.register(userName, passWord, 2);
        if (registerType) {
            return true;
        } else {
            throw new TTUException(Advice.REGISTER_ERROR);
        }
    }

    @Override
    public boolean update(User user, String userName, String passWord) {
        //先判断是否为管理员权限
        if (user.getPower() != 1) {
            throw new TTUException(Advice.INSUFFICIENT_PERMISSIONS);
        } else {
            boolean updateType = userMapper.update(user.getId(), userName, passWord);
            if (updateType) {
                return true;
            }else {
                throw new TTUException(Advice.REGISTER_ERROR);
            }
        }
    }
}
